Ext.ns('cn.christ.web.action.dict.area');
var areaForm = cn.christ.web.action.dict.area.AreaForm;

areaForm = function() {
	var formPanel, win, swfupload;
	var impFormPanel, impWin;

	return {

		createFormPanel : function(v) {
			if(v == 'imp'){
				impFormPanel = new Ext.form.FormPanel({
					id : 'impFormPanel',
					name : 'impFormPanel',
					labelAlign : 'right',
					labelWidth : 65,
					padding : '10',
					defaults : {       //字段的公共属性
						anchor : '90%' //字段宽度占容器宽度的比例
					},
					items : [
						{
							xtype : "container",
							layout : "column",
							defaults : {
								border : false
							},
							items : [{
										width : 230,
										style : "padding-left:0px;",
										layout : "form",
										items : {
											xtype : "textfield",
											fieldLabel : "Excel文件",
											id : 'excelFileName',
											name : "excelFilePath",
											readOnly : true,
											anchor : "98%,98%"
										}
									}, {
										xtype : "button",
										text : "上传",
										iconCls : "uploadIcon",
										scope : areaForm,
										handler : function() {
											this.createSwfUpload();
											swfupload.show();
										}
									}, {
										xtype : "button",
										text : "删除",
										style : "padding-left:5px;",
										iconCls : "remove",
										handler : function() {
											var idvl = Ext.getCmp('excelFileName').getValue();
											if(idvl && idvl!=''){
												var id_vl = idvl.split('|');
												Ext.getCmp('excelFileName').setValue('');
												Ext.Ajax.request({
													url : cfg.cxt + '/attach_delete.do',
													success : function(response) {
														var rt = Ext.decode(response.responseText);
														if (!rt.success){
															safetys.ajaxError(response);
														} 
													},
													failure : safetys.ajaxError,
													params : {
														"ids" : id_vl[0]
													}
												});
											}
										}
									}]
						}
					]
				});
				return impFormPanel;
				
			}else{
				formPanel = new Ext.form.FormPanel({
						id : 'formPanel',
						name : 'formPanel',
						labelAlign : 'right',
						labelWidth : 65,
						padding : "10",
						defaultType : 'textfield',
						defaults : {
							anchor : '90%'
						},
						items : [{
									fieldLabel : '区域名称',
									id : 'name',
									name : 'name',
									maxLength:32,
									allowBlank : false
								}, {
									fieldLabel : '区域代码',
									id : 'code',
									name : 'code',
									maxLength:32,
									allowBlank : false
								}, {
									fieldLabel : '区域简称',
									id : 'shortName',
									name : 'shortName'
								},{
									id : 'windowmode',
									name : 'windowmode',
									hidden : true
								}, {
									id : 'id',
									fieldLabel:'编号',
									name : 'id',
									hidden : true
								},{
									id : 'fatherId',
									name : 'parent.id',
									hidden : true
								}, {
							    fieldLabel : '上级区域',
								id:'areaName',
								name:'areaName',
								xtype : 'displayfield'
							},{
									xtype:'textarea',
									fieldLabel : '区域说明',
									id:'txtRemark',
									maxLength:100,
									name : 'remark',
									allowBlank : true
								},{
									id : 'sort',
									fieldLabel:'序号',
									name : 'sort',
									maxLength:5,
									regex: /^[1-9]*$/,
									regexText : '请输入正确的序号'
									 
								}]
					});
				return formPanel;
			}
			
		},

		createWindow : function(v) {
			if(v == 'imp'){
				impWin = new ExtWin(
					{
						width : 420,
						height : 120,
						items : impFormPanel,
						buttons : [{
							text : '确认',
							iconCls : 'acceptIcon',
							handler : function() {
								listView.subFileForm('/area_imp.do');
							}
						}, {
							text : '关闭',
							iconCls : 'deleteIcon',
							handler : function() {
								impWin.hide();
							}
						}]
					}
				);
				return impWin;
			}else{
				win = new ExtWin({
						width : 420,
						height : 350,
						items : formPanel,
						buttons : [{
							text : '保存',
							iconCls : 'acceptIcon',
							handler : function() {
								var f = formPanel.getForm();
								if (f.isValid()) {
									var mode = Ext.getCmp('windowmode').getValue();
									if (mode == 'add') {
										listView.subForm('/area/add.action');
									} else {
										listView.subForm('/area/update.action');
									}
								}
							}
						}, {
							text : '关闭',
							iconCls : 'deleteIcon',
							handler : function() {
								win.hide();
							}
						}]
					});
				return win;
			}
		},
		
		createSwfUpload : function(){
			if( !swfupload ) {
				swfupload = new SwfWin({
					uploadConfig : {
						fileSize : '10MB',
						border : false,
						singleFile : true,//是否上传队列中只能放置一个文件
						fileTypes : '*.xls;*.xlsx', // 在这里限制文件类型:'*.*'
						fileTypesDescription : 'Excel文件'
					},
					buttons : [
								{
									text : '确认',
									iconCls : 'acceptIcon',
									handler : function() {
									    	var ds  = UploadPanel.grid.getStore();
									    	var upnum =  ds.getCount();
									    	if( upnum == 0){
									    		Ext.Msg.alert('提示','上传列表中，不存在您要上传的文件！');
									    		return;
									    	}
									    	//因为是单文件上传，上传列表只存在一条记录 
									    	var r = ds.getAt(0);
									    	if (r.data.fileState == SWFUpload.FILE_STATUS.QUEUED
													|| r.data.fileState == SWFUpload.FILE_STATUS.IN_PROGRESS) {
									    		Ext.Msg.alert('提示','您所选的文件尚未上传！');
												return;
											}
									    	if(r.data.fileState == SWFUpload.FILE_STATUS.ERROR){
									    		Ext.Msg.alert('提示','文件未上传成功！');
												return;
									    	}
											var id = r.get('fileDbId');
											var path = r.get('fileServerPath');
											Ext.getCmp('excelFileName').setValue(id+'|'+path);
											swfupload.hide();
									}
								} ,{
									text : '取消',
									iconCls : 'deleteIcon',
									handler : function() {
										swfupload.hide();
									}
								} ]
				});
				
				swfupload.on('show',function(){
					 UploadPanel.grid.getStore().removeAll();
				});
				
			}
			return swfupload;
		} 

	}
}();